package com.ziytek.web.citizen.service.impl;

import com.ziytek.web.citizen.config.ConfigInit;
import com.ziytek.web.citizen.configuration.RefreshSqliteConfiguration;
import com.ziytek.web.citizen.service.itf.SqliteSwitchService;
import com.ziytek.web.citizen.util.DateTimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * sqlite数据库切换实现管理类
 *
 * @author fenghx
 * @version 1.0
 * @className SqliteSwitchServiceImpl
 * Description:
 * @date 2024/3/15 15:52
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SqliteSwitchServiceImpl implements SqliteSwitchService {
    private final ConfigInit configInit;
    private final RefreshSqliteConfiguration refreshSqliteConfiguration;

    @Override
    public void switchDb() {
        var switchSuccess = configInit.isDbCharge();
        if (!switchSuccess) {
            return;
        }

        var startTimestamp = System.currentTimeMillis();

        var dbName = "Data_" + DateTimeUtils.toDate(System.currentTimeMillis() - 60 * 60 * 1000L, "yyMMdd");
        refreshSqliteConfiguration.updateYmlData("historyDbName", dbName);

        log.info("切換数据库文件完成,耗时:{}ms", System.currentTimeMillis() - startTimestamp);
    }
}
